jQuery(document).ready(function($) {
    jQuery.noConflict();
    
    // add User validation
    $("#addWarehouse").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            warehousename: "required",
            warehouseaddress: "required",
            customerdefinedname: "required"
            
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    $("#addUser").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            username: {
                minlength: 6,
                required: true
            },
            email: {
                email: true
            },
            password:{
                minlength: 6,
                required: true
            }
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    
    // add Engineer validation
    $("#addEngineer").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            engname: "required",
            engcell: {
                 required : true,
                 minlength : 11,
                 digits : true
            }
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    
    // add employee item validation
    $("#addEmployee").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            employeename: "required",
            employeecell: {
                 required : true,
                 minlength : 11,
                 digits : true
            }
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    
    
    // add Customer item validation
    $("#addCustomer").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            cusname: "required",
            cellnumber: {
                 required : true,
                 minlength : 11,
                 digits : true
            }
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    
    // add Project item validation
    $("#addProject").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            projectname: "required"
            
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    
    // add stockin item validation
    $("#addStockIn").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            serialno: "required",
            productid: "required",
            revisionstate: "required",
            in_quantity:{
                required : true,
                digits : true
            },
            shiptype: "required",
            rackno: "required",
            invoicenumber: "required",
            shipid: "required",
            mawb: "required",
            hawb: "required",
            receiveddate: "required",
            rma: "required",
            manufacturingdate: "required",
            pickuprequestdate: "required",
            siteid: "required",
            docketdate: "required",
            shipfrom: "required",
            docketnumber: "required",
            boxno: "required"
        },
        submitHandler: function(form) {
            $.post("add?action=AddStockIn",$("form#addStockIn").serialize(), function(data){
                $( 'form#addStockIn' ).each(function(){
                     this.reset();
                 });
                $("#show").html(data);
            });
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    // add stockout item validation
    $("#addStockOut").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            productid: "required",
            serialno: "required",
            boxno: "required",
            out_docketnumber: "required",
            revisionstate: "required",
            challannumber: "required",
            receiveddate: "required",
            deliverydate: "required",
            out_quantity:{
                 required : true,
                 digits : true
            },
            requestnumber: "required",
            requestedby: "required",
            siteid: "required",
            project_id: "required",
            requesttime: "required",
            deliverytime: "required",
            receivedtime: "required",
            receivername: "required",
            cellnumber: {
                 required : true
                 },
            transportername: "required"            
        },
        submitHandler: function(form) {
            $.post("add?action=AddStockOut",$("form#addStockOut").serialize(), function(data){
                $( 'form#addStockOut' ).each(function(){
                     this.reset();
                 });
                //$("#show").html(data);
                location.reload();
            });
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    // modify stockout item validation
    $("#modifyStockOut").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            challannumber: "required",
            siteid: "required",
            deliverytime: "required",
            receivername: "required",
            cellnumber: {
                 required : true,
                 minlength : 11,
                 digits : true
                }
        },
        submitHandler: function(form) {
            $.post("modify?action=ModifyStockout",$("form#modifyStockOut").serialize(), function(data){
                $( 'form#modifyStockOut' ).each(function(){
                     this.reset();
                 });
                 window.location = "modify-stockout.php";
            });
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function( index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function( index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    // add Return item validation
    $("#addFaulty").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            return_rma: "required",
            pickuprequestdate: "required",
            physicallyreceivedate: "required",
            productid: "required",
            boxno: "required",
            serialno: "required",
            revisionstate: "required",
            project_id: "required",
            return_quantity: {
                 required : true,
                 digits : true
                },
            manufacturingdate: "required",
            return_docketnumber: "required",
            siteid: "required",
            receivedfrom: "required",
            returningstatus: "required",
            rr_rma: "required",
            export_rma: "required",
            rr_invoiceno: "required",
            rr_awb: "required",
            rr_mawb: "required"
            
        },
        submitHandler: function(form) {
            $.post("add?action=AddFaulty",$("form#addFaulty").serialize(), function(data){
                $( 'form#addFaulty' ).each(function(){
                     this.reset();
                 });
                $("#show").html(data);
            });
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    
    // add Faulty Export item validation
    $("#addFaultyExport").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            productid: "required",
            boxno: "required",
            serialno: "required",
            revisionstate: "required",
            project_id: "required",
            faulty_export_quantity:{
                 required : true,
                 digits : true
                },
            handedoverto: "required",
            faultydocketnumber: "required",
            siteid: "required",
            receivedfrom: "required",
            rr_rma: "required",
            export_rma: "required",
            rr_invoiceno: "required",
            rr_awb: "required",
            rr_mawb: "required"            
        },
        submitHandler: function(form) {
            $.post("add?action=AddExport",$("form#addFaultyExport").serialize(), function(data){
                $( 'form#addFaultyExport' ).each(function(){
                     this.reset();
                 });
                $("#show").html(data);
            });
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    // Bug Report validation
    $("#addBug").validate({
        onfocusout: function(element, event) {
            this.element(element);
        },
        onkeyup: false,
        rules: {
            title: "required",
            description: "required"
        },
        showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "right",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-content\"><p></p></div></div>"
                });
                _popover.data("popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
    // all date and time formats in the forms
    $(function() {
        $('#receiveddate, #deliverytime, #requesttime, #receivedtime, #physicallyreceivedate, #pickuprequestdate, #docketdate, #rr_date, #startdate, #enddate').datetimepicker({
            format: 'yyyy-MM-dd hh:mm:ss',
            language: 'en',
            pick12HourFormat: true
        });
        
    });    
    
    $("#response_type_id").blur(function() {
       var responsetime = $("#response_type_id").val();
       jQuery.post("check.php?action=ResponseTime&responsetime="+responsetime, function(data) {
           $("#sla").val(data);
       });
       
    });
    $("#stockin_serial").blur(function() {
        var sn = $("#stockin_serial").val();
        jQuery.post("check?action=InCheck&InSerial="+sn, function(data) {
            var obj = $.parseJSON(data);
            $("#productid").val(obj['productid']);
            $("#project_id").val(obj['project_id']);
            $("#description").val(obj['description']);
            $("#boxno").val(obj['boxno']);            
            $("#revisionstate").val(obj['revisionstate']);
            $("#uom").val(obj['uom']);
            $("#mawb").val(obj['mawb']);
            $("#hawb").val(obj['hawb']);
            $("#receiveddate").val(obj['receiveddate']);
            $("#invoicenumber").val(obj['invoicenumber']);
            $("#shipid").val(obj['shipid']);
            $("#shipfrom").val(obj['shipfrom']);
            $("#rackno").val(obj['rackno']);
            $("#docketnumber").val(obj['docketnumber']);
            $("#manufacturingdate").val(obj['manufacturingdate']);
            $("#pickuprequestdate").val(obj['pickuprequestdate']);
            $("#employee_id").val(obj['employee_id']);
            $("#eng_id").val(obj['eng_id']);
            $("#warehouse_id").val(obj['warehouseid']);
            $("#remarks").val(obj['remarks']);
        });
    });
    
    $("#stockout_serial").blur(function() {
        var sn = $("#stockout_serial").val();
        jQuery.post("check?action=Check&Serial="+sn, function(data) {
            var obj = $.parseJSON(data);
            $("#productid").val(obj['productid']);
            $("#stockin_id").val(obj['stockin_id']);
            $("#boxno").val(obj['boxno']);
            $("#description").val(obj['description']);
            $("#revisionstate").val(obj['revisionstate']);
            $("#uom").val(obj['uom']);
            $("#project_id").val(obj['project_id']);
            $("#warehouse_id").val(obj['warehouseid']);
            $("#remarks").val(obj['remarks']);
        });
    });
    
    $("#faulty_in_sn").blur(function() {
        var sn = $("#faulty_in_sn").val();
        jQuery.post("check?action=FaultyCheck&SerialF="+sn, function(data) {
            var obj = $.parseJSON(data);
            $("#productid").val(obj['productid']);
            $("#stockin_id").val(obj['stockin_id']);
            $("#boxno").val(obj['boxno']);
            $("#return_quantity").val(obj['out_quantity']);
            $("#manufacturingdate").val(obj['manufacturingdate']);
            $("#siteid").val(obj['siteid']);
            $("#receivedfrom").val(obj['receivedfrom']);
            $("#docketdate").val(obj['docketdate']);
            $("#faulties_docketnumber").val(obj['faulties_docketnumber']);
            $("#description").val(obj['description']);
            $("#revisionstate").val(obj['revisionstate']);
            $("#uom").val(obj['uom']);
            $("#project_id").val(obj['project_id']);
            $("#warehouse_id").val(obj['warehouseid']);
            $("#remarks").val(obj['remarks']);
        });
    });
    
    $("#faulty_export_sn").blur(function() {
        var fen = $("#faulty_export_sn").val();
        jQuery.post("check.php?action=ExportCheck&ExportSerial="+fen, function(data1) {
            var obj = $.parseJSON(data1);
            if(obj) {
                $("#project_id").val(obj['project_id']);
                $("#productid").val(obj['productid']);
                $("#boxno").val(obj['boxno']);
                $("#description").val(obj['description']);
                $("#revisionstate").val(obj['revisionstate']);
                $("#manufacturingdate").val(obj['manufacturingdate']);
                $("#uom").val(obj['uom']);
                $("#faulty_export_quantity").val(obj['return_quantity']);
                $("#siteid").val(obj['siteid']);
                $("#warehouse_id").val(obj['warehouseid']);
                $("#remarks").val(obj['remarks']);
            } else {
                $("#project_id, #productid, #boxno, #description, #manufacturingdate").attr("disabled", "disabled");
            }
        });
    });
    $("#pickSearchTerm").blur(function() {
        var searchterm = $("#pickSearchTerm").val();
        $.post("generator.php?action=Movement&st="+searchterm, function(data) {
            $("#mhbyproductid").html(data);
        });
    });
    $("#pickSearchSerialNo").blur(function() {
        var serialNo = $("#pickSearchSerialNo").val();
        $.post("generator.php?action=MovementBySerial&stforSearial="+serialNo, function(data) {
            jQuery("#mhbyserialno").html(data);
        });
    });
    $("#stockinhand").click(function() {
        $.post("generator.php?action=StockInHand", function(data) {
            jQuery("#stockinhanddiv").html(data);
        });
    });
    
    //$.post("index.php",$("#LoginActionForm").serialize(),function(data){
    // password generator
    $('div.password').easypassgen();
    
    $("#DateBetween").click(function() {
        
        var $from = $("#startdate").val();
        var $to = $("#enddate").val();
        $.post("generator?action=DateBetweenSI&From="+$from+"&To="+$to, function(data) {
            jQuery("#stockinhanddiv").html(data);
        });
    });
});
function employee(id) {
    jQuery.post("erase.php?id="+id, function(data) {
        jQuery("#load").html(data);
    });
}
function employeemodify(id) {
    jQuery.post("modify.php?action=EmployeeModify&id="+id, function(data) {
        jQuery("#load").html(data);
    });
}
function deletestockin(stockin_id) {
    //alert(stockin_id);
    jQuery.post("erase?action=eraseSI&siid="+stockin_id, function(data) {
        //alert(stockin_id);
        jQuery("#show").html(data);
    });
}
function deletestockout(stockout_id) {
    //alert(stockin_id);
    jQuery.post("erase?action=eraseSO&soid="+stockout_id, function(data) {
        //jQuery("#show").html(data);
        location.reload();
    });
}
function deletereturn(faulties_id) {
    //alert(stockin_id);
    jQuery.post("erase?action=eraseReturn&faulties_id="+faulties_id, function(data) {
        //alert(stockin_id);
        jQuery("#show").html(data);
    });
}
function deleteexport(faulty_export_id) {
    //alert(stockin_id);
    jQuery.post("erase?action=eraseExport&faulty_export_id="+faulty_export_id, function(data) {
        //alert(stockin_id);
        jQuery("#show").html(data);
    });
}

function savestockin(stockin_id) {
    jQuery.post("modify?action=modifySI&siid="+stockin_id, function(data) {
        jQuery("#show").html(data);
    });
}
function savestockout(stockout_id) {
    jQuery.post("modify?action=modifySO&soid="+stockout_id, function(data) {
        //jQuery("#show").html(data);
        location.reload();
    });
}
function savereturn(faulties_id) {
    jQuery.post("modify?action=modifyReturn&faulties_id="+faulties_id, function(data) {
        jQuery("#show").html(data);
    });
}
function saverexport(faulty_export_id) {
    jQuery.post("modify?action=modifyExport&faulty_export_id="+faulty_export_id, function(data) {
        jQuery("#show").html(data);
    });
}
function receivedok(note_id) {
    jQuery.post("modify?action=modifyReturnNote&note_id="+note_id, function(data) {
        jQuery("#showstatus").html(data);
    });
}
function okayBoss(reportid) {
    jQuery.post("modify?action=modifyBugReport&reportid="+reportid, function(data) {
        location.reload();
    });
}
